<?php

class WinCron {

    static $incr;
    static $cronfile;

    public static function init($cronfile) {
        ignore_user_abort();
        set_time_limit(0);
        self::$incr = 0;
        self::$cronfile = $cronfile;
        $pingQQ = ping('www.qq.com');
        $pingBaidu = ping('www.baidu.com');
        if ($pingQQ < 0 || $pingBaidu < 0) {
            log::dw('Ping Failed,Please Check Http NetWork!');
            sleep(5);
            return self::init($cronfile);
        } else {
            log::notice('$pingQQ :' . $pingQQ);
            log::notice('$pingBaidu  :' . $pingBaidu);
        }
    }

    public static function run() {
        log::notice('Incr:' . self::get());
        self::parse();
        return self::$incr++;
    }

    public static function get() {
        return self::$incr;
    }

    public static function parse() {
        if (!file_exists(self::$cronfile)) {
            log::dw('Can NOT Find The Crontab File:' . self::$cronfile);
        } else {
            $arrCronConfig = file(self::$cronfile);
            foreach ($arrCronConfig as &$v) {
                $v = trim($v);
                if (!$v || substr($v, 0, 1) == '#') {
                    continue;
                }
                $arrLine = explode('|', $v);
                if (self::get() % $arrLine[0] == 0 && self::get() != 0) {
                    log::notice('Start Exec:' . $arrLine[1]);
                    self::WScript_Shell($arrLine[1]);
                    log::notice('End Exec:' . $arrLine[1]);
                }
            }unset($v);
        }
    }

    public static function WScript_Shell($str_cmd) {
        $result = true;
        try {
            $obj_ws = new COM('WScript.Shell');
            $obj_ws->Run('%comspec% /c ' . $str_cmd, 0, false);
        } catch (Exception $e) {
            $result = $e->getMessage();
        }
        return $result;
    }

}

?>